﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using Swsk33.SimpleArcEngine.Dialog;
using System.Windows.Forms;

namespace Swsk33.SimpleArcEngine.Util
{
	/// <summary>
	/// 框选要素工具类
	/// </summary>
	public class SelectQueryFeature : ICommand, ITool
	{
		/// <summary>
		/// 被操作地图对象
		/// </summary>
		private AxMapControl map;

		public void OnCreate(object Hook)
		{
			map = (AxMapControl)Hook;
		}

		public bool Enabled
		{
			get
			{
				return true;
			}
		}

		public bool Checked
		{
			get
			{
				return true;
			}
		}

		public string Name
		{
			get;
			set;
		}

		public string Caption
		{
			get;
			set;
		}

		public string Tooltip
		{
			get;
			set;
		}

		public string Message
		{
			get;
			set;
		}

		public string HelpFile
		{
			get;
			set;
		}

		public int HelpContextID
		{
			get;
			set;
		}

		public int Bitmap
		{
			get;
			set;
		}

		public string Category
		{
			get;
			set;
		}

		public void OnClick()
		{

		}

		public void OnMouseDown(int button, int shift, int x, int y)
		{
			// 获取在地图上画的矩形框
			IGeometry geometry = map.TrackRectangle();
			// 新建查询器
			ISpatialFilter filter = new SpatialFilter();
			// 设定查询范围为全部字段
			filter.SubFields = "*";
			// 设定查询的几何的范围为我们划定的矩形框范围
			filter.Geometry = geometry;
			// 查询Shape
			filter.GeometryField = "shape";
			// 查询与之相交的要素
			filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
			// 创建窗口
			SelectQueryDialog dialog = new SelectQueryDialog(map);
			for (int i = 0; i < map.LayerCount; i++)
			{
				IFeatureLayer layer = (IFeatureLayer)map.get_Layer(i);
				IFeatureCursor cursor = layer.Search(filter, false);
				IFeature feature = null;
				while ((feature = cursor.NextFeature()) != null)
				{
					ListViewItem item = dialog.resultList.Items.Add(layer.Name);
					item.SubItems.Add(feature.OID.ToString());
				}
			}
			dialog.ShowDialog();
		}

		public void OnMouseMove(int button, int shift, int x, int y)
		{

		}

		public void OnMouseUp(int button, int shift, int x, int y)
		{

		}

		public void OnDblClick()
		{

		}

		public void OnKeyDown(int keyCode, int shift)
		{

		}

		public void OnKeyUp(int keyCode, int shift)
		{

		}

		public bool OnContextMenu(int x, int y)
		{
			return true;
		}

		public void Refresh(int hdc)
		{

		}

		public bool Deactivate()
		{
			return true;
		}

		public int Cursor
		{
			get;
			set;
		}
	}
}